package cn.iocoder.yudao.module.jikong.controller.admin.project;

import cn.iocoder.yudao.module.jikong.controller.admin.area.vo.JkAreaPageReqVO;
import cn.iocoder.yudao.module.jikong.controller.admin.area.vo.JkAreaRespVO;
import cn.iocoder.yudao.module.jikong.dal.dataobject.area.JkAreaDO;
import cn.iocoder.yudao.module.jikong.service.area.JkAreaService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.jikong.controller.admin.project.vo.*;
import cn.iocoder.yudao.module.jikong.dal.dataobject.project.JkProjectDO;
import cn.iocoder.yudao.module.jikong.service.project.JkProjectService;

@Tag(name = "管理后台 - 项目列表")
@RestController
@RequestMapping("/jikong/jk-project")
@Validated
public class JkProjectController {

    @Resource
    private JkProjectService jkProjectService;
    @Resource
    private JkAreaService jkAreaService;

    @PostMapping("/create")
    @Operation(summary = "创建项目列表")
    @PreAuthorize("@ss.hasPermission('jikong:project:create')")
    public CommonResult<Long> createJkProject(@Valid @RequestBody JkProjectSaveReqVO createReqVO) {
        return success(jkProjectService.createJkProject(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目列表")
    @PreAuthorize("@ss.hasPermission('jikong:project:update')")
    public CommonResult<Boolean> updateJkProject(@Valid @RequestBody JkProjectSaveReqVO updateReqVO) {
        jkProjectService.updateJkProject(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目列表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('jikong:project:delete')")
    public CommonResult<Boolean> deleteJkProject(@RequestParam("id") Long id) {
        jkProjectService.deleteJkProject(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除项目列表")
                @PreAuthorize("@ss.hasPermission('jikong:project:delete')")
    public CommonResult<Boolean> deleteJkProjectList(@RequestParam("ids") List<Long> ids) {
        jkProjectService.deleteJkProjectListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目列表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('jikong:project:query')")
    public CommonResult<JkProjectRespVO> getJkProject(@RequestParam("id") Long id) {
        JkProjectDO jkProject = jkProjectService.getJkProject(id);
        return success(BeanUtils.toBean(jkProject, JkProjectRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得项目列表分页")
    @PreAuthorize("@ss.hasPermission('jikong:project:query')")
    public CommonResult<PageResult<JkProjectRespVO>> getJkProjectPage(@Valid JkProjectPageReqVO pageReqVO) {
        PageResult<JkProjectDO> pageResult = jkProjectService.getJkProjectPage(pageReqVO);

        List<Long> collect = pageResult.getList().stream().map(JkProjectDO::getAreaId).collect(Collectors.toList());
        //根据区域id查询区域名称
        List<JkAreaDO> areaList = jkAreaService.getListByIds(collect);
        //转为map
        Map<Long, String> areaMap = areaList.stream().collect(Collectors.toMap(JkAreaDO::getId, JkAreaDO::getName));

        return success(BeanUtils.toBean(pageResult, JkProjectRespVO.class, jkProjectDO -> {
            jkProjectDO.setAreaName(areaMap.get(jkProjectDO.getAreaId()));
        }));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得项目列表下拉框")
    public CommonResult<PageResult<JkProjectRespVO>> getJkProjectSimpleList(@Valid JkProjectPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<JkProjectDO> pageResult = jkProjectService.getJkProjectPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JkProjectRespVO.class));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出项目列表 Excel")
    @PreAuthorize("@ss.hasPermission('jikong:project:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportJkProjectExcel(@Valid JkProjectPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<JkProjectDO> list = jkProjectService.getJkProjectPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "项目列表.xls", "数据", JkProjectRespVO.class,
                        BeanUtils.toBean(list, JkProjectRespVO.class));
    }

}
